﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class AdminHome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindLatestApplications();
                BindLatestBJXXApplications();
                BindLatestTasks();
                BindZBChart();
            }
        }

        private void BindZBChart()
        {
            ZBTJService service = new ZBTJService();
            ZBTJBackgroundService service1 = new ZBTJBackgroundService();
            //int t5tat = service.Get0ToXCountTATCountOfCurrentMonth(5); // 改成后台获取
            //int t3tat = service.Get0To3CountOfCurrentMonth();
            int t5tat = service1.GetOTo5TAT();
            int t3tat = service1.GetOTo3TAT();
            int wxl = service.GetDYWXL();
            int bjStatisfy = service.GetBJStatisfyByMonth(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Month);
            int bjxxStatisfy = service.GetBJXXStatisfyByMonth(DateTime.Now.Year, DateTime.Now.Month);
            T5TATImage.ImageUrl = string.Format("https://chart.googleapis.com/chart?cht=p&chd=t:{0},{1}&chco=00FF00|0000FF&chtt=0-5%20%E5%A4%A9TAT%3A{0}%25&chts=000066,12&chs=150x150&chf=bg,s,65432100", t5tat, 100 - t5tat);
            T3TATImage.ImageUrl = string.Format("https://chart.googleapis.com/chart?cht=p&chd=t:{0},{1}&chco=00FF00|0000FF&chtt=0-3%20%E5%A4%A9TAT%3A{0}%25&chts=000066,12&chs=150x150&chf=bg,s,65432100", t3tat, 100 - t3tat);
            CompleteRateImage.ImageUrl = string.Format("https://chart.googleapis.com/chart?cht=p&chd=t:{0},{1}&chco=00FF00|0000FF&chtt=%E5%BD%93%E6%9C%88%E5%AE%8C%E4%BF%AE%E7%8E%87%3A{0}%25&chts=000066,12&chs=150x150&chf=bg,s,65432100", wxl, 100 - wxl);
            BJStatisfyRateImage.ImageUrl = string.Format("https://chart.googleapis.com/chart?cht=p&chd=t:{0},{1}&chco=00FF00|0000FF&chtt=备件满足率:{0}%&chts=000066,12&chs=150x150&chf=bg,s,65432100", bjStatisfy, 100 - bjStatisfy);
            BJXXStatisfyRateImage.ImageUrl = string.Format("https://chart.googleapis.com/chart?cht=p&chd=t:{0},{1}&chco=00FF00|0000FF&chtt=备件先行满足率:{0}%&chts=000066,12&chs=150x150&chf=bg,s,65432100", bjxxStatisfy, 100 - bjxxStatisfy);
            rmaCountLbl.Text = service.GetRMACountOfMonth(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Month).ToString();
            doaCountLbl.Text = service.GetDOACountOfMonth(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Month).ToString();
        }

        private void BindLatestApplications()
        {
            ApplicationService applicationService = new ApplicationService();
            ApplicationGridView.DataSource = applicationService.GetLatestApplications();
            ApplicationGridView.DataBind();
        }

        private void BindLatestTasks()
        {
            TaskService taskService = new TaskService();
            TaskGridView.DataSource = taskService.GetLatestTasks(ContextService.CurrentAccount);
            TaskGridView.DataBind();
        }

        private void BindLatestBJXXApplications()
        {
            ApplicationService applicationService = new ApplicationService();
            BJXXGridView.DataSource = applicationService.GetLatestBJXXApplications();
            BJXXGridView.DataBind();
        }

        protected void ApplicationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void TaskGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void GoTOApplicationDetailLnk_OnClick(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("ApplicationDetail.aspx?{0}={1}&BackType=2", Constants.QUERYSTR_APPLICATION_ID, ((LinkButton)(sender)).CommandArgument));
        }

        protected void GoTOTaskDetailLnk_OnClick(object sender, EventArgs e)
        {
            TaskService service = new TaskService();
            Task task = service.GetTaskByTaskID(Convert.ToInt32(((LinkButton)(sender)).CommandArgument));
            if (task.Status.Equals("已处理"))
            {
                Response.Redirect(string.Format("RWGLDetailReadOnly.aspx?{0}={1}", "TID", ((LinkButton)(sender)).CommandArgument));
            }
            else
            {
                Response.Redirect(string.Format("RWGLDealWithForm.aspx?{0}={1}&BackType=2", "TID", ((LinkButton)(sender)).CommandArgument));
            }
        }

        protected void UpdateTimer_Tick(object sender, EventArgs e)
        {
            BindLatestApplications();
            BindLatestTasks();
            BindLatestBJXXApplications();
        }
    }
}
